home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2001 Spring / Oh!X 2001 Spring Special CD-ROM (Japan).7z / Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin / ERI / eri2bmp.cpp < prev    next >
C/C++ Source or Header  |  2000-12-01  |  6KB  |  255 lines

  1.  
  2. /*****************************************************************************
  3.                   恵理ちゃん → ビットマップ 変換プログラム
  4.                                                                 2000/11/27
  5.  -----------------------------------------------------------------------------
  6.            Copyright (C) 2000 Leshade Entis. All rights reserved.
  7.  *****************************************************************************/
  8.  
  9.  
  10. //
  11. // ヘッダの読み込み
  12. //////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <math.h>
  19. #include <memory.h>
  20.  
  21. typedef    signed short int    SWORD ;
  22. typedef    signed long int        SDWORD ;
  23. typedef    unsigned __int64    UINT64 ;
  24.  
  25. #include "experi.h"
  26.  
  27.  
  28. //
  29. // 入力コンテキストの宣言と定義
  30. //////////////////////////////////////////////////////////////////////////////
  31.  
  32. class    MyDecodeContext    : public    RLHDecodeContext
  33. {
  34.     ERIFile *    m_pfile ;
  35.  
  36. public:
  37.     // 構築関数
  38.     MyDecodeContext( ERIFile * pfile )
  39.         : RLHDecodeContext( 0x1000 ), m_pfile( pfile ) { }
  40.     // データ入力関数
  41.     virtual ULONG ReadNextData( PBYTE ptrBuffer, ULONG nBytes ) ;
  42.  
  43. } ;
  44.  
  45. ULONG MyDecodeContext::ReadNextData( PBYTE ptrBuffer, ULONG nBytes )
  46. {
  47.     return    m_pfile->Read( ptrBuffer, nBytes ) ;
  48. }
  49.  
  50.  
  51. //
  52. // メモリアロケーション
  53. //////////////////////////////////////////////////////////////////////////////
  54.  
  55. PVOID eriAllocateMemory( DWORD dwBytes )
  56. {
  57.     return    malloc( dwBytes ) ;
  58. }
  59.  
  60. void eriFreeMemory( PVOID pMemory )
  61. {
  62.     free( pMemory ) ;
  63. }
  64.  
  65.  
  66. //
  67. // エントリポイント
  68. //////////////////////////////////////////////////////////////////////////////
  69.  
  70. int main( int argc, char * argv[] )
  71. {
  72.     //
  73.     // プログラム見出し表示
  74.     //
  75.     printf( "恵理ちゃん → ビットマップ 変換プログラム\n" ) ;
  76.     printf( "Copyright (C) 2000 Leshade Entis. All rights reserved.\n" ) ;
  77.     printf( "\n" ) ;
  78.  
  79.     if ( argc < 3 )
  80.     {
  81.         //
  82.         // 書式表示
  83.         //
  84.         printf( "書式 : eri2bmp <ERIファイル> <BMPファイル>\n" ) ;
  85.         printf( "\n" ) ;
  86.         printf( "    ERIファイル : 変換するERIファイル名を指定します。\n" ) ;
  87.         printf( "    BMPファイル : 変換後のBMPファイル名を指定します。\n" ) ;
  88.         printf( "\n" ) ;
  89.         return    0 ;
  90.     }
  91.  
  92.     //
  93.     // ERIファイルを開く
  94.     //
  95.     EReadFile    rf ;
  96.     if ( !rf.Open( argv[1] ) )
  97.     {
  98.         printf( "\"%s\" を開けませんでした。\n\n", argv[1] ) ;
  99.         return    1 ;
  100.     }
  101.     ERIFile    erif ;
  102.     if ( !erif.Open( &rf ) )
  103.     {
  104.         printf( "\"%s\" は恵理ちゃんではありません。\n\n", argv[1] ) ;
  105.         return    2 ;
  106.     }
  107.  
  108.     //
  109.     // ビットマップを展開するための準備
  110.     //
  111.     RASTER_IMAGE_INFO    rii ;
  112.     rii.fdwFormatType = erif.m_InfoHeader.fdwFormatType ;
  113.     rii.nImageWidth = erif.m_InfoHeader.nImageWidth ;
  114.     rii.nImageHeight = abs( erif.m_InfoHeader.nImageHeight ) ;
  115.     rii.dwBitsPerPixel = erif.m_InfoHeader.dwBitsPerPixel ;
  116.     rii.BytesPerLine =
  117.         ((rii.nImageWidth * rii.dwBitsPerPixel + 0x1f) & ~0x1f) >> 3 ;
  118.     rii.ptrImageArray =
  119.         (PBYTE) malloc( rii.BytesPerLine * rii.nImageHeight ) ;
  120.     //
  121.     printf( "現在展開中です・・・" ) ;
  122.  
  123.     //
  124.     // 展開開始
  125.     //
  126.     ERIDecoder    decoder ;
  127.     MyDecodeContext    context( &erif ) ;
  128.     if ( decoder.Initialize( erif.m_InfoHeader ) )
  129.     {
  130.         printf( "初期化に失敗しました。\n" ) ;
  131.         free( rii.ptrImageArray ) ;
  132.         return    3 ;
  133.     }
  134.     if ( decoder.DecodeImage( rii, context, false ) )
  135.     {
  136.         printf( "失敗しました。\n" ) ;
  137.         free( rii.ptrImageArray ) ;
  138.         return    4 ;
  139.     }
  140.     //
  141.     printf( "終了しました。\n" ) ;
  142.  
  143.     //
  144.     // BMPファイルを開く
  145.     //
  146.     FILE *    bmpf = fopen( argv[2], "wb" ) ;
  147.     if ( bmpf == NULL )
  148.     {
  149.         printf( "\"%s\" を開けませんでした。\n\n", argv[2] ) ;
  150.         free( rii.ptrImageArray ) ;
  151.         return    5 ;
  152.     }
  153.  
  154.     bool    fError = false ;
  155.     printf( "現在書き出し中です・・・" ) ;
  156.     do
  157.     {
  158.         //
  159.         // ビットマップファイルヘッダを書き出す
  160.         //
  161.         int    pltlen = 0 ;                    // パレットの長さ
  162.         if ( rii.dwBitsPerPixel <= 8 )
  163.         {
  164.             pltlen = (1 << rii.dwBitsPerPixel) ;
  165.         }
  166.         BITMAPFILEHEADER    bmfh ;
  167.         bmfh.bfType = *((WORD*)"BM") ;
  168.         bmfh.bfOffBits = sizeof(BITMAPFILEHEADER)
  169.                         + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * pltlen ;
  170.         bmfh.bfReserved1 = 0 ;
  171.         bmfh.bfReserved2 = 0 ;
  172.         bmfh.bfSize = bmfh.bfOffBits + rii.BytesPerLine * rii.nImageHeight ;
  173.         if ( fwrite( &bmfh, sizeof(bmfh), 1, bmpf ) < 1 )
  174.         {
  175.             fError = true ;
  176.             break ;
  177.         }
  178.         //
  179.         // ビットマップ情報ヘッダを書き出す
  180.         //
  181.         BITMAPINFOHEADER    bmih ;
  182.         memset( &bmih, 0, sizeof(bmih) ) ;
  183.         bmih.biSize = sizeof(bmih) ;
  184.         bmih.biWidth = rii.nImageWidth ;
  185.         bmih.biHeight = rii.nImageHeight ;
  186.         bmih.biPlanes = 1 ;
  187.         bmih.biBitCount = (WORD) rii.dwBitsPerPixel ;
  188.         bmih.biCompression = BI_RGB ;
  189.         bmih.biSizeImage = rii.BytesPerLine * rii.nImageHeight ;
  190.         if ( fwrite( &bmih, sizeof(bmih), 1, bmpf ) < 1 )
  191.         {
  192.             fError = true ;
  193.             break ;
  194.         }
  195.         //
  196.         // パレットテーブルを書き出す
  197.         //
  198.         if ( pltlen > 0 )
  199.         {
  200.             RGBQUAD    rgbTable[0x100] ;
  201.             if ( rii.fdwFormatType & ERI_WITH_PALETTE )
  202.             {
  203.                 memcpy( rgbTable,
  204.                     erif.m_PaletteTable, sizeof(RGBQUAD) * pltlen ) ;
  205.             }
  206.             else
  207.             {
  208.                 BYTE    step = (BYTE)(1 << (8 - rii.dwBitsPerPixel)) ;
  209.                 BYTE    value = 0 ;
  210.                 for ( int i = 0; i < pltlen; i ++ )
  211.                 {
  212.                     rgbTable[i].rgbBlue = value ;
  213.                     rgbTable[i].rgbGreen = value ;
  214.                     rgbTable[i].rgbRed = value ;
  215.                     rgbTable[i].rgbReserved = 0 ;
  216.                     value += step ;
  217.                 }
  218.             }
  219.             if ( fwrite( rgbTable,
  220.                         sizeof(RGBQUAD), pltlen, bmpf ) < (size_t) pltlen )
  221.             {
  222.                 fError = true ;
  223.                 break ;
  224.             }
  225.         }
  226.         //
  227.         // ビットマップ配列を書き出す
  228.         //
  229.         if ( fwrite( rii.ptrImageArray, bmih.biSizeImage, 1, bmpf ) < 1 )
  230.         {
  231.             fError = true ;
  232.             break ;
  233.         }
  234.     }
  235.     while ( false ) ;
  236.     //
  237.     fclose( bmpf ) ;
  238.     //
  239.     if ( fError )
  240.     {
  241.         printf( "失敗しました。\n" ) ;
  242.         free( rii.ptrImageArray ) ;
  243.         return    6 ;
  244.     }
  245.     printf( "終了\n\n" ) ;
  246.  
  247.     //
  248.     // 後始末
  249.     //
  250.     free( rii.ptrImageArray ) ;
  251.  
  252.     return    0 ;
  253. }
  254.  
  255.